package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.AssemblyBuilding;
import com.system.runtai.entity.CommandSystem;
import com.system.runtai.entity.EmergencyEquipment;
import com.system.runtai.service.CommandSystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 指挥系统 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-04-05
 */
@Api(tags = "指挥系统")
@RestController
@RequestMapping("/runtai/command-system")
public class CommandSystemController {

    @Autowired
    private CommandSystemService commandSystemService;

    @ApiOperation(value = "所有指挥系统")
    @GetMapping("/commandSystemList")
    public Result<List<CommandSystem>> commandSystemList(){
        List<CommandSystem> list = commandSystemService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "指挥系统详情")
    @GetMapping("/commandSystemById/{id}")
    public Result<CommandSystem> commandSystemById(@PathVariable Integer id){
        CommandSystem byId = commandSystemService.getById(id);
        return Result.ok(byId);
    }

    @ApiOperation(value = "添加指挥系统")
    @PostMapping("/saveCommandSystem")
    public Result<CommandSystem> saveCommandSystem(@RequestBody CommandSystem commandSystem){

        commandSystem.setCreateTime(LocalDateTime.now());
        boolean save = commandSystemService.save(commandSystem);

        if (save){
            return Result.ok(commandSystem).message("添加成功");
        }else {
            return Result.ok(commandSystem).message("添加失败");
        }
    }

    @ApiOperation(value = "删除指挥系统")
    @PostMapping("delectCommandSystem/{id}")
    public Result delectCommandSystem(@PathVariable Integer id){

        boolean b = commandSystemService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "修改指挥系统")
    @PostMapping("/updateCommandSystem")
    public Result<CommandSystem> updateCommandSystem(@RequestBody CommandSystem commandSystem){

        commandSystem.setUpdateTime(LocalDateTime.now());
        boolean update = commandSystemService.updateById(commandSystem);

        if (update){
            return Result.ok(commandSystem).message("修改成功");
        }else {
            return Result.ok(commandSystem).message("修改失败");
        }
    }

    @ApiOperation(value = "分页查询指挥系统")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CommandSystem>> indexdas(@PathVariable Long page, @PathVariable Long limit, CommandSystem commandSystem){

        Page<CommandSystem> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CommandSystem> wrapper = new LambdaQueryWrapper<>();

        Integer id = commandSystem.getId();

        if (id!=null){
            wrapper.eq(CommandSystem::getId,id);
        }

        IPage<CommandSystem> iPage = commandSystemService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }

    @ApiOperation(value = "指挥系统名称筛选")
    @GetMapping("/filkte")
    public Result<IPage<CommandSystem>> filtecarName(@RequestParam(required = false) String carName,@RequestParam(required = false) Integer limit,@RequestParam(required = false) Integer page){

        List<CommandSystem> list = null;
        if (carName!=null) {
        LambdaQueryWrapper<CommandSystem> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(CommandSystem::getCarname,carName);
        list = commandSystemService.list(wrapper);


            Page<CommandSystem> recommendPage = new Page<>(page,limit);
            IPage<CommandSystem> iPage = commandSystemService.page(recommendPage);
            return Result.ok(iPage);
        }else if (carName==null){
            list = commandSystemService.list();


            Page<CommandSystem> recommendPage = new Page<>(page,limit);
            IPage<CommandSystem> iPage = commandSystemService.page(recommendPage);
            return Result.ok(iPage);
        }
        Page<CommandSystem> recommendPage = new Page<>(page,limit);
        IPage<CommandSystem> iPage = null;
        return Result.fail(iPage);
    }

}

